<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防抖节流</title>
</head>
<body>
    <button>防抖</button>
    <button>节流</button>

</body>
<script>
    bt=document.getElementsByTagName('button')[0]
    bt2=document.getElementsByTagName('button')[1]

    function buy(){
        console.log('买了',this)
    }
    function debounce(func,delay){
        let timer;
        return function(){
            let context=this
            clearTimeout(timer)
            timer=setTimeout(func.bind(context),delay)
        }
    }
    bt.addEventListener('click',debounce(buy,1000))

    // function restart(){
    //     console.log('重新来了',this)
    // }
    // function throttle(func,delay){
    //     let timer
    //     return function(){
    //         if(timer){
    //             return
    //         }
    //         let context=this
    //         timer=setTimeout(()=>{
    //             func.apply(context);
    //             timer=null
    //         },delay)
    //     }
        
    // }
    // bt2.addEventListener('click',throttle(restart,1000))
</script>
</html>